﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;

namespace GestionPedago.Persistence.OPManager
{
    /// <summary>
    /// Manager de persistance de l'entity Contact
    /// </summary>
    public abstract class ContactOPManager
    {
        /// <summary>
        /// Méthode de récupération de la liste des contact par codeEntreprise
        /// </summary>
        /// <param name="codeEntreprise">Le code de l'entreprise à appliquer au filtre</param>
        /// <returns>La liste des contacts</returns>
        public static List<Contact> GetListContacts(int? codeEntreprise)
        {
            IQueryable<Contact> query = EntityConnection.GetContext().Contact;

            // Filtre sur codeEntreprise
            if (codeEntreprise != null) {
               query = query.Where(it => it.CodeEntreprise == codeEntreprise.Value);
            }

            // Tri sur le nom et retour de la liste
            return query.OrderBy(it => it.Nom).ThenBy(it => it.Prenom).ToList();
        }

        /// <summary>
        /// Méthode de récupération d'un contact par son code
        /// </summary>
        /// <param name="codeContact">Le code du contact à rechercher</param>
        /// <returns>Le contact trouvé</returns>
        public static Contact GetContactByCode(int codeContact)
        {

            try
            {
                var query = from it in EntityConnection.GetContext().Contact
                            where it.CodeContact == codeContact
                            select it;

                Contact e = null;

                e = query.First();

                return e;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }
}
